/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.heathrow.entities;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 *
 * @author Martin Žák
 */
@Entity
@Table(name = "systemUser")
public class User implements Serializable
{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(nullable = false, unique = true)
    private String username;
    @Column(nullable = true)
    private String passwordHash;
    @Column(nullable = false)
    @ElementCollection(fetch = FetchType.EAGER)
    private Set<String> roles;

    public void setId(Long id)
    {
        this.id = id;
    }

    public Set<String> getRoles()
    {
        return Collections.unmodifiableSet(roles);
    }

    public void setRoles(Set<String> roles)
    {
        this.roles = new HashSet<>(roles);
    }

    public Long getId()
    {
        return id;
    }

    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }

    public String getPasswordHash()
    {
        return passwordHash;
    }

    public void setPasswordHash(String passwordHash)
    {
        this.passwordHash = passwordHash;
    }

    @Override
    public int hashCode()
    {
        int hash = 7;
        hash = 71 * hash + Objects.hashCode(this.id);
        return hash;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        final User other = (User) obj;
        if (!Objects.equals(this.id, other.id))
            return false;
        return true;
    }

    @Override
    public String toString()
    {
        return "User{" + "id=" + id + ", username=" + username + '}';
    }
}
